#!/bin/bash

# Kill any previously running brokers
#pkill -9 acquirea

# Remove outstanding work stores
rm ./_*

# Remove all databases
rm ./*.sql

# Remove all workare files and results
rm -fr ./workarea_files/*
rm -fr ./results_files/*
#rm -fr ./hosted_files/*
rm -f *.pub
rm -f brokerkeyname
rm -f output.xml
rm -f work.xml

# Rebuild the broker with built in passwords
make clean depclean
cmake .
PASSDEFINE=-DPASSDEFINE make -j8

#if [ -n `which gsed 2>&1 | grep -v 'no gsed'` ]; then
#   SED=gsed
#else
   SED=sed
#fi

echo "start the broker now!"
# Start the broker in the background
#valgrind ./acquirea &

# Sleep and wait for keys to generate and stuff to come up
#sleep 25
sleep 5

# Download brokerkey for compute - and client
wget http://127.0.0.1:10000/hosted_files/brokerkeyname -O brokerkeyname
export BROKERKEY=`cat brokerkeyname`
wget http://127.0.0.1:10000/hosted_files/$BROKERKEY.pub -O $BROKERKEY.pub

#sleep 5
# Generate keydb with own key pair for client
./newclient -u myname

# Insert the public broker key from the compute into the client key database
echo "insert into \"public_key\" values('`cat brokerkeyname`', 'RSA', '`cat $BROKERKEY.pub`" >> ./client_keydb.sql
echo "', 'broker', 'Y', '1', 'Sun, 08 Jul 2012 09:28:31 GMT');" >> ./client_keydb.sql

echo "stop here for manual run, 5 seconds"
sleep 5

#sleep 5
# Initial user registration
./newclient -u myname -R

#sleep 1
# Download security from localhost and add user
export KVAL=`curl http://127.0.0.1:10000/console.html | grep "<input" \
| gawk ' { print $3 } ' | $SED 's:": :g' | head -n 1 | gawk ' { print $2 } '`

#sleep 1
# Upload 'allow' command
curl -X POST -d "$KVAL"=Allow http://127.0.0.1:10000/console.html

echo "you can stop here and let the keys change for the broker"
echo "10 seconds"
sleep 10

#export INDIREC=/home/chaders/mview-src-0.3.2
#export INDIREC=/home/benlong/bokeh
export INDIREC=$1

#sleep 1
# Make new xml descriptor
./newclient -u myname -x output.xml -d $INDIREC -C

#sleep 1
# Build stream description
export SESSIONID=`./newclient -u myname -x output.xml -d $INDIREC -V | tee ./__outputdata.txt | grep "WORKSTOREID" | gawk ' { print $2 } '`
cat ./__outputdata.txt
rm ./__outputdata.txt

#sleep 1
# Do upload
./newclient -u myname -x output.xml -d $INDIREC -s $SESSIONID -U

# Clean the compute output
rm -fr $PWD/compute_output/*

#sleep 1
# Start a compute node
#valgrind
echo "Going to start the compute node now, 10 seconds"
sleep 10
./compute -d $PWD/workarea_files

#sleep 5
#pkill -9 memcheck

#while [ 1 ];
#do
   export FINISHEDWORK=`./newclient -u myname -Q | tee ./__outputdata.txt | grep "3: 100\.0" | sed 's/://g' | gawk ' { print $2 } '`
   cat ./__outputdata.txt
   rm ./__outputdata.txt
   ./newclient -u myname -s $FINISHEDWORK -E
   export FINISHEDSESSION=`./newclient -u myname -s $FINISHEDWORK -I | tee ./__outputdata.txt | grep "DOWNLOADID" | gawk ' { print $2 } '`
   cat ./__outputdata.txt
   rm ./__outputdata.txt
#   export FINISHEDSESSION=`./newclient -u myname -s $FINISHEDWORK -D | grep -v "From" | grep ": $FINISHEDWORK" | $SED 's/://g' | $SED 's/,//g' | gawk ' { print $1 } '`
#   echo "FINISHEDWORK: " $FINISHEDWORK
#   echo "FINISHEDSESSION: " $FINISHEDSESSION
   ./newclient -u myname -s $FINISHEDWORK -l $FINISHEDSESSION -d $PWD/results_files -D
#done

echo "Phase 1 complete! - results downloaded"


